package exam

import (
    "github.com/astaxie/beego/orm"
	_ "github.com/mattn/go-sqlite3"
	"beego/testApp/common/tool"
	"time"
)

type Exam struct {
 
	Eid int  `orm:"pk;auto;column(eid)"`
	Ename string
	Cid int
	Created time.Time
}

func init(){
   
	tool.Pic("Exam pakcage init"," init")
  
	orm.RegisterModel(new(Exam))


	/*
	orm.RegisterDriver("sqlite", orm.DRSqlite)
    orm.RegisterDataBase("default", "sqlite3", "./db/admin.db")
	orm.RunSyncdb("default", false, true)
	*/

}


func (e *Exam) ExamListByCid() []Exam{

	o := orm.NewOrm()
	qs := o.QueryTable("exam")

	var exams []Exam

	num,err :=  qs.Filter("cid",e.Cid).All(&exams)
	
	if(err!=nil){
		tool.Pic("ExamListByCid get ERROR" ,err )
	   }
	tool.Pic("ExamListByCid  get  success" , num )

  return exams
	  
}


func (e *Exam) Get( eid string ) Exam {

	o := orm.NewOrm()
	qs := o.QueryTable("exam")

	var ex Exam

	num,err :=  qs.Filter("eid",eid).All(&ex)
	
	if(err!=nil){
	 tool.Pic("Exam get ERROR" ,err )
	}

	tool.Pic("exam  get  success" , num )


	return ex
	//stu := &Student{}
	//stu.Cid = cid
}




func (e *Exam)   Add() bool {

	o := orm.NewOrm()
	o.Using("default")
	id,err  := o.Insert(e)
	
	if(err!=nil){
	   
		tool.Pic("Exam add() insert into Db FAIL",err)
        return false
	} 

	tool.Pic("Exam add() insert into db succsess ",id)

   return true
}




